
jsx3.Class.defineClass(
        "ux.megatron.gui.SteppedWizard",
        ux.megatron.gui.Wizard,
        null,
        function (block) {

			
            /**
             * instance initializer
             * @param strName {String} unique name distinguishing this object from all other JSX GUI objects in the JSX application
             * @param vntLeft {int|String} either a number (i.e, 12, 30, etc) or a number and a unit value (i.e., "25%", "36pt", etc); if a number is passed, pixels will be the assumed unit when painted to screen
             * @param vntTop {int|String} either a number (i.e, 12, 30, etc) or a number and a unit value (i.e., "25%", "36pt", etc); if a number is passed, pixels will be the assumed unit when painted to screen
             * @param vntWidth {int|String} either a number (i.e, 12, 30, etc) or a number and a unit value (i.e., "25%", "36pt", etc); if a number is passed, pixels will be the assumed unit when painted to screen
             * @param vntHeight {int|String} either a number (i.e, 12, 30, etc) or a number and a unit value (i.e., "25%", "36pt", etc); if a number is passed, pixels will be the assumed unit when painted to screen
             * @param strHTML {String} Text/HTML markup to place in the jsx3.gui.Block instance
             */
            block.prototype.init = function(strName, vntLeft, vntTop, vntWidth, vntHeight, strHTML) {
                //call constructor for super class (the super expects the name of the object and the function that it is an instance of)
                this.jsxsuper(strName, vntLeft, vntTop, vntWidth, vntHeight, strHTML);
            };
						
						
						block.prototype.onAfterAttach = function(){
							this.setEnableStepJump(true);
							this.jsxsuper();
						}
			
			//modification by alan to add more functionality
			block.prototype.getMessageLabel = function(){
				return this.getDescendantOfName('lblMessage');
			};
			
			block.prototype._prepareStepJump = function(stepPane){
				var stepsPane = this.getStepsPane();
				if(stepsPane && this.getEnableStepJump() == true) {
					var children = stepsPane.getChildren();
					
					for(var i=0; i<children.length; i++) {
						var child = stepsPane.getChild(i);
						if(child.instanceOf("ux.megatron.gui.WizardStep")) {
							
							var script = 'this.getAncestorOfType("ux.megatron.gui.SteppedWizard").doStepJump(this.getAncestorOfType("ux.megatron.gui.WizardStep"));';
							//var script = 'alert(this.getAncestorOfType("ux.megatron.gui.SteppedWizard"))';
							var lbl = child.getDescendantOfName('lblSetActions_90');
							lbl.setCursor('pointer',true);
							lbl.setEvent(script,jsx3.gui.Interactive.JSXCLICK);
						}
					}
					//warning!! you must keep this repaint to make jsxclick of lable take effect.don't remove repaint for performace optimization purpose.
					this.repaint();
				}
			}
			block.prototype.setEnableStepJump = function(val){
				this.enablestepjump = val;
				this._prepareStepJump();
			}
			
			block.prototype.getEnableStepJump = function(val){
				return this.enablestepjump;
			}

			//jump to specified step
			block.prototype.doStepJump = function(wizardStep){
				var curr = this.getCurrentPanel();
        if(curr) {
            var me = this;
            var nextName = curr.doEvent("wizardjump", {objWIZARD:me,objSTEP:(wizardStep ? wizardStep: null),objEVENT:null,strCURRENTPANEL:curr.getName()});
            if(nextName && nextName == "NONE") {
							return;
            }
        }

				wizardStep.setActive();
				var idx = wizardStep.getChildIndex();
				var targetContent = this.getContentPane().getChildren()[idx];
				this.jumpToPanel(null, targetContent.getName());
			}
			
			//add a custom step to wizard
			block.prototype.addStep = function(stepIndex,stepUrl,stepContentUrl, namespace){
				
				var step = ux.megatron.Main.loadUIResource(this.getStepsPane(), namespace, stepUrl,true);
				var content = ux.megatron.Main.loadUIResource(this.getContentPane(), namespace, stepContentUrl,true);
				//re-arrange the step index by requirment.
				var stepsPane = this.getStepsPane();
				var contentPane = this.getContentPane();
				var children = stepsPane.getChildren();
				for(var i=0; i<children.length; i++) {
					if(i == stepIndex){
						var child = stepsPane.getChild(i);
						stepsPane.insertBefore(step,child,true);
						contentPane.insertBefore(content,contentPane.getChild(i),true);
						break;
					}
				}
				this.repaint();
				if(stepIndex == 0){
					this.jumpToPanel(null, content.getName());
					step.setActive();
				}
				
			}
			
			block.prototype.removeStep = function(stepIndex, reset){
				if(this.getContentPane() == null || this.getContentPane().getChildren().length < 1){
					jsx3.log('There is less than 2 content panel in wizard,so you can not perform a remove step operation');
					return;
				}
				var current = this.getCurrentPanel();
				var target = this.getContentPane().getChild(stepIndex);
				if(target == null){
					jsx3.log('There is no step for specified step index,check your code again please');
					return;
				}
				this.getStepsPane().removeChild(stepIndex);
				this.getContentPane().removeChild(stepIndex);
				if(current != target){
					
				}else{
					this.jumpToPanel(null, this.getContentPane().getChild(0).getName());
					this.getStepsPane().getChild(0).setActive();
				}
                if(reset) this.resetNumber();
			}

            block.prototype.resetNumber = function() {
                var stepsPane = this.getStepsPane();
                if(stepsPane) {
                    var children = stepsPane.getChildren();
                    for(var i=0; i<children.length; i++) {
                        var child = stepsPane.getChild(i);
                        if(child.instanceOf("ux.megatron.gui.WizardStep") && child.getChild(0) && child.getChild(0).setText) {
                            child.getChild(0).setText(i+1, true);
                        }
                    }
                }
            }
			
            block.prototype.setActiveStep = function(stepPanel) {
                this.activeStep = stepPanel;
            }

            block.prototype.nextStep = function() {
                var pane = this.getStepsPane();
                if(pane) {
                    try {
                    var idx = this.activeStep.getChildIndex();
                    var next = null;
                    for(var i = idx + 1; i < pane.getChildren().length; i++) {
                        var child = pane.getChild(i);
                        if(child.instanceOf("ux.megatron.gui.WizardStep")) {
                            next = child;
                            break;
                        }
                    }
                        var str = next + "";
                        try {
                            str = next.getName();
                        } catch(ex){}
                    if(next) {
                        this.activeStep = next;
                        next.setActive();
                    }
                    }catch(ex) {
                        ux.megatron.Main.logException(ex);
                        //do nothing
                    }
                }
                return this.activeStep;
            }

            block.prototype._findNextPanel = function() {
                var next = this.jsxsuper();
                if(next) {
                    this.nextStep();
                }
                return next;
            }

            block.prototype._findPrevPanel = function() {
                var prev = this.jsxsuper();
                if(prev) {
                    this.prevStep();
                }
                return prev;
            }

            block.prototype.prevStep = function() {
                var pane = this.getStepsPane();
                if(pane) {
                    try {
                        var idx = this.activeStep.getChildIndex();
						var prev = null;
						for(var i = idx - 1; i >= 0; i--) {
							var child = pane.getChild(i);
							if(child.instanceOf("ux.megatron.gui.WizardStep")) {
								prev = child;
								break;
							}
						}
						if(prev) {
							this.activeStep = prev;
							prev.setActive();
						}
                    }catch(ex) {
                        //do nothing
                    }
                }
                return this.activeStep;
            }

            block.prototype.onSetParent = function(objChild, intPersist, strSourceURL, strNS) {
				var ret = this.jsxsuper(objChild, intPersist, strSourceURL, strNS);
				try {
					var stepsPane = this.getStepsPane();
					var firstStep = null;
					if(stepsPane) {
						var children = stepsPane.getChildren();
						for(var i=0; i<children.length; i++) {
							var child = stepsPane.getChild(i);
							if(child.instanceOf("ux.megatron.gui.WizardStep")) {
								firstStep = child;
								break;
							}
						}
						if(firstStep) {
							this.activeStep = firstStep;
							firstStep.setActive();
						}
						//code for enable step jump
						this._prepareStepJump();
						
					}
				} catch(ex) {
					ux.megatron.Main.logException(ex);
				}

				
								
                return ret;
            }

            block.prototype.activateStep = function(strStepName) {
                var step = this.getStepPane().getDescendantOfName(strStepName, true);
                if(step) {
                    this.activeStep = step;
                    step.setActive();
                }
            }

            block.prototype.setStepsPaneName = function(paneName) {
                delete this.stepsPaneName;
                delete this.stepsPane;
                this.stepsPaneName = paneName;
                if(this.stepsPaneName) {
                    this.stepsPane = this.getDescendantOfName(stepsPaneName);
                }
                return this.stepsPane;
            }

            block.prototype.getStepsPaneName = function() {
                return this.stepsPaneName;
            }

            block.prototype.getStepsPane = function() {
                if(!this.stepsPane && this.stepsPaneName) {
                    this.stepsPane = this.getDescendantOfName(this.stepsPaneName);
                }
                return this.stepsPane;
            }

            block.prototype.setDetailsPaneName = function(paneName) {
                delete this.detailsPaneName;
                delete this.detailsPane;
                this.detailsPaneName = paneName;
                if(this.detailsPaneName) {
                    this.detailsPane = this.getDescendantOfName(detailsPaneName);
                }
                return this.detailsPane;
            }

            block.prototype.getDetailsPaneName = function() {
                return this.detailsPaneName;
            }

            block.prototype.getDetailsPane = function() {
                if(!this.detailsPane && this.detailsPaneName) {
                    this.detailsPane = this.getDescendantOfName(this.detailsPaneName);
                }
                return this.detailsPane;
            }

			//modify by alan.for extract the code from wizard component file
			block.prototype.initLeftInfo = function(){
				var objJSX = this;
				if(objJSX.rule && objJSX.rule.ruleName != undefined){

					try{
						objJSX.getDescendantOfName('lblNameCont_90').setText(objJSX.rule.ruleName,true);
				       	objJSX.getDescendantOfName('lblSchlCont_90').setText(objJSX.rule.ruleScheduleName,true);
				    	objJSX.getDescendantOfName('lblDescCont_90').setText(objJSX.rule.ruleDescription,true);
				    	objJSX.getDescendantOfName('lblAlertCont_90').setText(objJSX.rule.ruleSendAlert,true);
				    	if(objJSX.rule.ruleLog == 'true'){
							objJSX.getDescendantOfName('lblLogCont_90').setText('Y',true);
						}else{
							objJSX.getDescendantOfName('lblLogCont_90').setText('N',true);
						}

						if(objJSX.rule.ruleInvokedService == 'true'){
							objJSX.getDescendantOfName('lblActionCont_90').setText('Yes-'+objJSX.rule.ruleServiceName,true);
						}else{
							objJSX.getDescendantOfName('lblActionCont_90').setText('No',true);
						}

				    	//objJSX.getDescendantOfName('lblConditionCont_90').setText(objJSX.rule.conditionString,true);
				    	objJSX.getDescendantOfName('lblTagObjCont_90').setText(objJSX.rule.ruleTargetObjectName,true);

						//render code for conditions
						if(!objJSX.rule.conditions){
							return;
						}
						var conditions = objJSX.rule.conditions;
				        var container = objJSX.getDescendantOfName('lblConditionCont_90');
				        while(container.getChildren().length > 0){
				        	container.removeChild(0);
				        }
				        for(var i = 0; i < conditions.length; i++){
				            var condition = conditions[i];
				            var c = condition.triggerCondition;
				            var s = c.metricDisplayName;
				            var pane = new jsx3.gui.Block();
				            pane.setWidth("100%");
				            pane.setHeight("30");
							container.setChild(pane);
							var layout = new jsx3.gui.LayoutGrid();
				            pane.setChild(layout);
				            layout.setCols("80%,20%");
							//for trigger metric
							var paneTrigger = new jsx3.gui.Block();
				            paneTrigger.setWidth("100%");
				            paneTrigger.setHeight("30");
				            paneTrigger.setText((i+ 1) + "." + s)
							layout.setChild(paneTrigger);
							//for clear condition icon
							c = condition.clearCondition;
							if(c){
								var paneIcon = new jsx3.gui.Block();
				            	paneIcon.setWidth("100%");
				            	paneIcon.setHeight("20");
								layout.setChild(paneIcon);
								var image = new jsx3.gui.Image();
				            	paneIcon.setChild(image);
				            	image.setSrc(ux.megatron.Main.getServer().getDynamicProperty("@spm_addin@clear_condition"));
							}

							container.repaint();
						}
					}catch(e){
						//here's a cratical bug,but I can't fix it now.I couldn't find the reason.so just keep silent fro demo.
						//alert(e);
					}


			    }
				//alert("end init left info");
			}
        }
        )